package com.example.venadaoa.activity.setting;

import com.example.venadaoa.R;
import com.example.venadaoa.base.BaseActivity;
import com.example.venadaoa.datamanager.ServerResult;
import com.example.venadaoa.datamanager.UserInfo;
import com.example.venadaoa.net.Api;
import com.example.venadaoa.net.NetUtils;
import com.example.venadaoa.tool.ToastHelp;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Dell on 2017/7/13.
 */

public class ModifyPwActivity extends BaseActivity implements OnClickListener {
    private UserInfo userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mPageName = "ModifyPwActivity";
        setContentView(R.layout.modify_pwd_view);
        userInfo = UserInfo.getUserInfor(context);
        // 返回键
        ImageView backBtn = (ImageView) findViewById(R.id.title_bar_btn_left);
        // 提交键
        ImageView uploadBtn = (ImageView) findViewById(R.id.title_bar_btn_right);
        backBtn.setOnClickListener(this);
        uploadBtn.setOnClickListener(this);

        TextView text = (TextView) findViewById(R.id.title31);
        text.setText(UserInfo.getUserInfor(context).getUserObject().getLastModifyPdTime());
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        int id = arg0.getId();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
        switch (id) {
            case R.id.title_bar_btn_left:
                finish();
                break;
            case R.id.title_bar_btn_right:
                if (isCanUpload()) {
                    UpdateLoadInfo();
                }
                break;
            default:
                break;
        }
    }

    private void UpdateLoadInfo() {
        EditText edit1 = (EditText) findViewById(R.id.editText1);
        Map<String, String> params = new HashMap<>();
        params.put("newPassword", userInfo.MD5(edit1.getText().toString()));

        Api.getModifyPW(context, params, UserInfo.class, new NetUtils.NetCallBack<ServerResult>() {
            @Override
            public void success(ServerResult rspData) {

            }

            @Override
            public void failed(String msg) {

            }
        });
    }


//    /*
//     * 传送数据到服务器
//     */
//    private class UpdateLoadInfo extends AsyncTask<String, Integer, String> {
//        // private ProgressDialog mProgress;
//        public UpdateLoadInfo() {
//
//        }
//
//        @Override
//        protected void onPreExecute() {
//            super.onPreExecute();
//        }
//
//        @Override
//        protected String doInBackground(String... params) {
//            String result = null;
//            HttpResponse response = null;
//            BufferedReader reader = null;
//            try {
//
//                ArrayList<NameValuePair> vs = new ArrayList<NameValuePair>();
//                EditText edit1 = (EditText) findViewById(R.id.editText1);
//                vs.add(new BasicNameValuePair("newPassword", userInfo.MD5(edit1.getText().toString())));
//
//                response = LinkHelp.post(URLS.getModifyPW(), vs, false);
//
//                if (response != null) {
//                    StatusLine line = response.getStatusLine();
//                    if (line.getStatusCode() == 200) {
//                        reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
//                        StringBuilder sb = new StringBuilder();
//                        for (String s = reader.readLine(); s != null; s = reader.readLine()) {
//                            sb.append(s);
//                        }
//                        reader.close();
//                        result = sb.toString();
//
//                        System.out.println(result);
//                    }
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            return result;
//        }
//
//        @Override
//        protected void onPostExecute(String result) {
//            JSONObject jsonObject;
//            try {
//                jsonObject = new JSONObject(result);
//                if (jsonObject.isNull("code")) {
//                    String status = jsonObject.getString("status");
//                    UserInfo info = UserInfo.getUserInfor();
//                    if (status.equals("success")) {
//                        Toast.makeText(ModifyPwActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
//                        EditText edit1 = (EditText) findViewById(R.id.editText1);
//                        userInfo.setPassword(edit1.getText().toString());
//                        finish();
//                    } else {
//                        String msg = jsonObject.getString("msg");
//                        //Toast.makeText(ModifyPwActivity.this, msg, Toast.LENGTH_SHORT).show();
//                        ToastHelp.doToastInTop(ModifyPwActivity.this, msg);
//                    }
//
//                }
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//        }
//
//    }

    public boolean isCanUpload() {
        EditText edit = (EditText) findViewById(R.id.editText);
        if (!userInfo.MD5(edit.getText().toString()).equals(userInfo.getUserObject().getPassWord())) {
            ToastHelp.doToastInTop(ModifyPwActivity.this, "密码验证错误");
            return false;
        }

        edit = (EditText) findViewById(R.id.editText1);
        EditText edit1 = (EditText) findViewById(R.id.editText2);
        if (edit.getText().toString().equals("")) {
            ToastHelp.doToastInTop(ModifyPwActivity.this, "密码不能为空");
            return false;
        }

        if (!edit.getText().toString().equals(edit1.getText().toString())) {
            ToastHelp.doToastInTop(ModifyPwActivity.this, "两次密码不一致");
            return false;
        }

        return true;
    }

}
